package cn.zmit.kuxi.holder;

import java.util.HashMap;
import java.util.Map;

import com.xdroid.common.utils.PreferenceHelper;
import com.xdroid.common.utils.ToastUtils;
import com.xdroid.functions.holder.ViewHolderBase;
import com.xdroid.functions.request.JsonData;
import com.xdroid.request.extension.XDroidRequest;
import com.xdroid.request.extension.config.DataType;
import com.xdroid.request.extension.impl.OnRequestListenerAdapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import cn.zmit.kuxi.R;
import cn.zmit.kuxi.activity.GoodsDetailActivity;
import cn.zmit.kuxi.activity.LoginActivity;
import cn.zmit.kuxi.activity.NowJoinActivity;
import cn.zmit.kuxi.activity.ShoppingCartsActivity;
import cn.zmit.kuxi.contants.SPConstants;
import cn.zmit.kuxi.contants.Url;
import cn.zmit.kuxi.entity.GoodsEnity;
import cn.zmit.kuxi.event.EventFactory;
import cn.zmit.kuxi.image.ImageDisplayer;
import cn.zmit.kuxi.interfaces.OnListItemSelectListener;
import cn.zmit.kuxi.request.RequestTask;

/** * @author chenxu @parameter * @return */
public class GoodsHolder extends ViewHolderBase<GoodsEnity> {
	private ImageView goods_pic;
	private TextView title, goodsName, allNeed, join_count;
	private ProgressBar progressBar;
	private Button joinCarts, nowJoin;
	private Context context;
	private RelativeLayout rlRoot;
	@SuppressLint("InflateParams")
	public static OnListItemSelectListener<GoodsEnity> onlistItemSelect;
	private String userid;
	Boolean haslog;

	@SuppressLint("InflateParams")
	@Override
	public View createView(LayoutInflater layoutInflater) {
		context = layoutInflater.getContext();
		View view = layoutInflater.inflate(R.layout.item_goods, null);
		goods_pic = (ImageView) view.findViewById(R.id.image_item_goods);
		title = (TextView) view.findViewById(R.id.tv_goods_title);
		goodsName = (TextView) view.findViewById(R.id.tv_goods_detail);
		allNeed = (TextView) view.findViewById(R.id.tv_goods_join);
		progressBar = (ProgressBar) view.findViewById(R.id.progressBar_goods);
		tv_goods_allneed = (TextView) view.findViewById(R.id.tv_goods_allneed);
		joinCarts = (Button) view.findViewById(R.id.btn_shopping_carts);
		nowJoin = (Button) view.findViewById(R.id.Btn_join);
		rlRoot = (RelativeLayout) view.findViewById(R.id.rl_root);
		haslog = PreferenceHelper.readBoolean(context,
				SPConstants.SHARED_PREFERENCE_NAME, SPConstants.HAS_LOGGED,
				false);
		if (haslog) {
			userid = PreferenceHelper.readString(context,
					SPConstants.SHARED_PREFERENCE_NAME, SPConstants.USERID, "");
		}

		return view;
	}

	private String flag;
	private String title2;
	private String goodName;
	private Double progress;
	private String allNeed2;
	private String joinCount;
	private String gp_id2;
	private TextView tv_goods_allneed;

	@Override
	public void showData(final int position, final GoodsEnity itemData) {
		System.out.println("position====" + position);
		if (itemData.getUrl() != null && itemData.getUrl().length() > 0) {
			ImageDisplayer.getInstance().dispalyImageWithRadius(
					Url.IMAGE_PICTURES + itemData.getUrl(), goods_pic, 0);
		} else {
			goods_pic.setImageResource(R.drawable.ic_launcher);
		}
		title.setText("第" + itemData.getTitle() + "期");
		goodsName.setText(itemData.getGoodsName());
		tv_goods_allneed.setText("总需人数：" + itemData.getAllNeed());
		allNeed.setText( "已参与：" + itemData.getJoinCount());

		double all = Integer.parseInt(itemData.getAllNeed());
		double join = Integer.parseInt(itemData.getJoinCount());
		final double result = ((join / all) * 100);
		progressBar.setMax(100);
		progressBar.setProgress((int) (result));
		String goodsId = itemData.getGoodsId();
		String gp_id = itemData.getGp_id();
		joinCarts.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (haslog) {
					Map<String, Object> map = new HashMap<String, Object>();
					map.put("user_id", userid);
					map.put("gp_id", itemData.getGp_id());
					map.put("number", "1");
					RequestTask.getInstance().doAddCarts((Activity) context,
							map, new onRequestListener());
				} else {
					Intent intent = new Intent();
					intent.setClass(context, LoginActivity.class);
					context.startActivity(intent);
				}

			}
		});
		rlRoot.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (onlistItemSelect != null) {
					onlistItemSelect.onlistItemSelect(position, itemData);
				}
			}
		});
		nowJoin.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("user_id", userid);
				map.put("gp_id", itemData.getGp_id());
				flag = "手机页面跳转";
				title2 = itemData.getTitle();
				goodName = itemData.getGoodsName();
				progress = result;
				allNeed2 = itemData.getAllNeed();
				joinCount = itemData.getJoinCount();
				gp_id2 = itemData.getGp_id();
				 Intent intent = new Intent();
				 Bundle bundle = new Bundle();
				 bundle.putString("flag", "手机页面跳转");
				 bundle.putString("title", itemData.getTitle());
				 bundle.putString("goodsName", itemData.getGoodsName());
				 bundle.putDouble("progress", result);
				 bundle.putString("allNeed", itemData.getAllNeed());
				 bundle.putString("joinCount", itemData.getJoinCount());
				 bundle.putString("gp_id", itemData.getGp_id());
				 bundle.putString("description", itemData.getDesc());
				 intent.putExtras(bundle);
				 intent.setClass(context, NowJoinActivity.class);
				 context.startActivity(intent);
			}
		});
	}


	private class onRequestListener extends OnRequestListenerAdapter<Object> {
		@Override
		public void onDone(XDroidRequest<?> request, String response,
				DataType dataType) {
			// TODO Auto-generated method stub
			super.onDone(request, response, dataType);
			JsonData jsonData = JsonData.create(response);
			String data = jsonData.optString("code");
//			String desc = jsonData.optString("description");
			if (data.equals("1")) {
				int count = jsonData.optInt("my_goods_count");
				EventFactory.sendUpdateCartCount(count);
//				ToastUtils.show(context, jsonData.optString("message"));
			} else {
//				ToastUtils.show(context, jsonData.optString("message"));
			}
		}
	}

	/**
	 * 设置回调
	 * 
	 */
	public static void setOnListItemSelectListener(
			OnListItemSelectListener<GoodsEnity> onListItemSelectListener) {
		GoodsHolder.onlistItemSelect = onListItemSelectListener;
	}
}
